package com.zimmur.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsOrganization;
import com.zimmur.model.goods.view.GoodsOrganizationView;
import com.zimmur.model.zone.view.IndexView;
import com.zimmur.service.IMakerService;
import com.zimmur.service.goods.IGoodsOrganizationService;
import com.zimmur.tools.PageUtil;
/**
 * 闪创接口
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value="/restful/organization")
public class OrganizationController extends BaseController {

	private static Logger log=LoggerFactory.getLogger(OrganizationController.class);
	@Value("#{spingMvcProperties['System.GuestId']}")
	private String guessUserId;
	@Resource
	private IGoodsOrganizationService goodsOrganizationService;
	 @Resource  
	 private IMakerService makeService;
	 
	@ResponseBody
    @RequestMapping(value="/index",method=RequestMethod.GET)  
    public ReturnJsonEntity index(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
		    Map<String,Object> map=goodsOrganizationService.selectIndex();
			entity.setData(map);   
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
       entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
   }
	@ResponseBody
    @RequestMapping(value="/indexPage",method=RequestMethod.GET)  
    public ReturnJsonEntity indexPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			PageUtil<GoodsOrganizationView> page=goodsOrganizationService.selectIndexPage(pageNow, null);
			entity.setData(page);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
       entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
   }
	@ResponseBody
    @RequestMapping(value="/queryGoods",method=RequestMethod.POST)  
    public ReturnJsonEntity queryGoods(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try{
			Integer pageNow = getIntParam("pageNow");
			String keyword=getParam("keyword");
			PageUtil<GoodsOrganizationView> page=goodsOrganizationService.selectIndexPage(pageNow, keyword);
			entity.setData(page);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
		entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
   }
	
	 /**
	 * 商品明细
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/goodsdetail",method=RequestMethod.GET)  
    public ReturnJsonEntity goodsDetail(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Boolean isUser = false;
		try{
			if (!String.valueOf(getTokenUserId()).equals(guessUserId)) {
				isUser = true;
			}
	        long goodsId = getLongParam("goodsId");
	        long userId= getTokenUserId();
	        Goods goods = makeService.goodsDetails(goodsId,userId,isUser);
	        GoodsOrganization goodsOrganization=goodsOrganizationService.selectByGoodsId(goodsId);
	        if(goods != null && goodsOrganization !=null){
	        	Map<String, Object> map=new HashMap<>();
	        	map.put("goods", goods);
	        	map.put("goodsOrganization", goodsOrganization);
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        	entity.setData(map);
	        }else{
	        	entity.setData(new Array());
	        	statusCodeEnum = StatusCodeEnum.CODE400404;
	        }
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
}
